<?php require('common/isLogin.php');?>
<?php require('common/mysql.php');?>
<?php  
// 获取cookie中的用户名  
$byuser = isset($_COOKIE['user']) ? $_COOKIE['user'] : '';  
$sqlObj = new Sql(); 
if($_GET['id']){
	$sql = "SELECT * FROM `account` where id = {$_GET['id']}";  
    $editZD = $sqlObj->get_row($sql);  
}
$catesql = "SELECT * FROM `category` where status = 1";  
$cate = $sqlObj->getAll($catesql);
if($_GET['del']){
    $sql = "DELETE FROM `account` WHERE id = {$_GET['del']}";
    $result = $sqlObj->query($sql);
    $sqlObj->close(); // 关闭数据库连接
    echo "<script>alert(\"删除成功！\");window.location.href = 'index.php'</script>";exit;
}
// 检查是否是POST请求  
if ($_SERVER['REQUEST_METHOD'] === 'POST') {  
    // 从POST数据中获取其他字段  
    $cate = $_POST['cate'] ?? '';  
    $money = $_POST['moneynum'];
    if($money=='0'){
        echo "<script>alert(\"金额不能为零！\");window.location.href = ''</script>";exit;
    }
    $zcsr = $_POST['zcsr'] ?? '';
    $fs = $_POST['fs'] ?? '';
    $beizhu = $_POST['beizhu'] ?? '';
    // 获取当前日期  
    $date = date('Y-m-d');  
  
    // 创建SQL插入语句  
    $sql = "INSERT INTO account (byuser, cate, date, money, zcsr, fs, beizhu) VALUES ('$byuser', '$cate', '$date', '$money', '$zcsr', '$fs', '$beizhu')";
	if($_GET['id']){
		$sql = "UPDATE `account` SET `cate` = '$cate', `money` = '$money', `zcsr` = '$zcsr', `fs` = '$fs', `beizhu` = '$beizhu' WHERE `account`.`id` = {$_GET['id']};";
	}
    // 实例化Sql类并执行插入操作   
    $result = $sqlObj->query($sql);  
  
    if ($result) {  
        echo "<script>alert(\"数据保存成功！\");window.location.href = ''</script>";  
    } else {  
        echo "<script>alert(\"数据保存失败！\");window.location.href = ''</script>";  
    }  
  
    $sqlObj->close(); // 关闭数据库连接  
}  
  
?>  
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title>Hadi.记账中心</title>
		<link rel="stylesheet" href="css/style.css" />
		<link rel="stylesheet" href="css/bootstrap.min.css" />
	</head>
	<body>
		<!--头部-->
		<div class="container-fluid header">
		  <div class="row hd_title">
			  记 录 一 笔 账 单
		  </div>
		</div>
		<!--白色计算信息-->
		<!--账单明细-->
		<div class="container allzdh">
			<!--这是账单行-->
			<form action="" method="post">
			<div class="row zdh jlzd">
				<!--这是一条账单start-->
				<div class="row onezdh">
					<div class="col">
						账单分类：
					</div>
					<div class="col">
						<select name="cate">
						<?php for($i=0;$i<count($cate);$i++){?>
							
							<option <?php if($editZD["cate"]==$cate[$i]['name']){echo 'selected';}?> value="<?php echo $cate[$i]['name'];?>"><?php echo $cate[$i]["name"];?></option>
						<?php }?>
						</select>
					</div>
				</div>
				<div class="row onezdh">
					<div class="col">
						金额：
					</div>
					<div class="col">
						<input type="text" value="<?php echo number_format($editZD["money"],2)?>" required placeholder="输入金额" name="moneynum" class="srje" />
					</div>
				</div>
				<div class="row onezdh">
					<div class="col">
						支出/收入：
					</div>
					<div class="col">
						<select name="zcsr">
							<option value="0">支出</option>
							<option value="1" <?php if($editZD["zcsr"]==1){echo 'selected';}; ?>>收入</option>
						</select>
					</div>
				</div>
                <div class="row onezdh">
                    <div class="col">
                        方式：
                    </div>
                    <div class="col">
                        <select name="fs">
                            <option value="0">微信</option>
                            <option value="1" <?php if($editZD["fs"]==1){echo 'selected';}; ?>>支付宝</option>
                        </select>
                    </div>
                </div>
                <div class="row onezdh">
                    <div class="col">
                        备注：
                    </div>
                    <div class="col">
                        <textarea name="beizhu" placeholder="输入账单备注（可空）" style="border-radius:3px;outline:none"><?php echo $editZD["beizhu"];?></textarea>
                    </div>
                </div>
                <div class="row onezdh">
                    <!--记账按钮-->
                    <div class="container text-center">
                        <div class="row justify-content-center">
                            <div class="col">
                                <button class="jzbtn">保存</button>
                            </div>
                        </div>
                    </div>
                </div>
                <?php if($_GET['id']){?>
                <div class="row onezdh">
                    <!--记账按钮-->
                    <div class="container text-center">
                        <div class="row justify-content-center">
                            <div class="col">
                                <button class="jzbtn" onclick="window.location.href='?del=<?php echo $_GET['id'];?>';return false;">删除账单</button>
                            </div>
                        </div>
                    </div>
                </div>
                <?php }?>
				<!--这是一条账单end-->
			</div>
			</form>
			<!--这是账单行end-->
		</div>
		<div class="container text-center" style="padding-top: 20px;letter-spacing: 2px;">
			<div class="row">
				<div class="col">
					<a href="addcate.php" style="color: rgb(153,153,153);font-size:14px;text-decoration: none;">没有你要的账单分类？点此申请</a>
				</div>
			</div>
		</div>
		<?php include('footer.php');?>
	</body>
</html>
